package com.example.shop.modules.manage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shop.modules.manage.service.IOrderAdminService;
import com.example.shop.modules.manage.service.impl.OrderDetailAdminServiceImpl;
import com.example.shop.persistence.entity.Goods;
import com.example.shop.persistence.entity.Order;
import com.example.shop.persistence.entity.OrderDetail;
import com.example.shop.persistence.mapper.ExpressMapper;
import com.example.shop.persistence.mapper.GoodsMapper;
import com.example.shop.persistence.mapper.OrderMapper;
import com.example.shop.utils.JSONResult;
import com.example.shop.utils.MPPageFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static com.example.shop.utils.JSONResult.renderSuccess;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author jason
 * @since 2019-12-17
 */
@Service
public class OrderAdminServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderAdminService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailAdminServiceImpl orderDetailService;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ExpressMapper expressMapper;

    /* 更新商品销量 和 库存 */
    public void updateGoods(Integer id, Integer num){
        Goods goods = goodsMapper.selectById(id);
        goods.setSellCount(goods.getSellCount() + num);
        Integer stock = goods.getStock() - num;
        if(stock < 0){
            stock = 0;
        }
        goods.setStock(stock);
        goodsMapper.updateById(goods);
    }

    /*删除*/
    public int deleteOrderInfo(String ids){
        // 先转换为字符串数组
        String[] strs = ids.split(",");
        List<Long> ints = new LinkedList<Long>();
        for(String id: strs){
            // 在转换为long 类型数组
            ints.add(Long.valueOf(id));
            QueryWrapper ew = new QueryWrapper();
            ew.eq("orderID", id);
            // 先删除订单详情表中数据
            orderDetailService.remove(ew);
            // 再删除快递信息
            expressMapper.delete(ew);
        }
        //批量删除
        return orderMapper.deleteBatchIds(ints);
    }

    /*分页查询全部信息*/
    public IPage<Order> selectOrderInfoList(Integer page, Integer pageSize, Integer state, Integer id, String phone, String startTime, String endTime){
        Page<Order> pages = MPPageFactory.createPage(page,pageSize);
        QueryWrapper ew = new QueryWrapper();
        if(id != null){
            ew.eq("id",id);
        }
        if(state != null){
            ew.eq("state", state);
            ew.orderByDesc("payTime");
        }
        if(StrUtil.isNotEmpty(phone)){
            ew.eq("phone",phone);
        }
        if(StrUtil.isNotEmpty(startTime)){
            ew.ge("createTime", startTime);
        }
        if(StrUtil.isNotEmpty(endTime)){
            ew.le("createTime", endTime);
        }
        ew.orderByDesc("createTime");
        // 返回分页类型 列表集合数据
        return orderMapper.selectPage(pages, ew);
    }

    /*订单详情*/
    public JSONResult selectOrderInfo(Integer id){
        return renderSuccess(orderMapper.selectOrderAll(id));
    }

    /* 更新订单状态和时间 */
    public int updateOrderInfo(Order order){
        if(order.getState() != null){
            switch (order.getState()){
                case 1000:
                    // 新增订单
                    break;
                case 1100:
                    // 付款
                    order.setPayTime(LocalDateTime.now());
                    break;
                case 1110:
                    // 发货
                    order.setDeliveryTime(LocalDateTime.now());
                    break;
                case 1120:
                    // 用户确认收货
                    order.setConfirmTime(LocalDateTime.now());
                    break;
                default:
                    // 交易完成
                    order.setCompleteTime(LocalDateTime.now());
            }
        }
        return orderMapper.updateById(order);
    }

}
